package untils
{
	import com.senocular.display.TransformTool;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;

	public class TransformTool2 extends Sprite
	{
		public static const CHANGED : String = "untils.TransformTool2_Changed";
		
		private var m_target : Sprite;
		
		private var m_lines : TransformLinesTool;
		
		private var m_topMid : TransformDotControl;
		private var m_bottomMid : TransformDotControl;
		private var m_rightTop : TransformDotControl;
		private var m_rightMid : TransformDotControl;
		private var m_rightBottom : TransformDotControl;
		private var m_leftTop : TransformDotControl;
		private var m_leftBottom : TransformDotControl;
		private var m_leftMid : TransformDotControl;
		private var m_center : TransformDotControl;
		
		private var m_strength : Boolean;
		private var m_strengthDot : DisplayObject;
		private var m_dragPoint : Point;
		
		public function TransformTool2()
		{
			createTool();
		}
		
		public function destory() : void
		{
			if(m_center)
			{
				this.m_center.removeEventListener(MouseEvent.MOUSE_DOWN,onCenterDown);
				this.m_center.removeEventListener(MouseEvent.MOUSE_UP,onCenterUp);
				
				m_topMid.removeEventListener(MouseEvent.MOUSE_DOWN,onDTopMid);
				m_bottomMid.removeEventListener(MouseEvent.MOUSE_DOWN,onDBottomMid);
				m_rightTop.removeEventListener(MouseEvent.MOUSE_DOWN,onDRightTop);
				m_rightMid.removeEventListener(MouseEvent.MOUSE_DOWN,onDRightMid);
				m_rightBottom.removeEventListener(MouseEvent.MOUSE_DOWN,onDRightBottom);
				m_leftTop.removeEventListener(MouseEvent.MOUSE_DOWN,onDLeftTop);
				m_leftMid.removeEventListener(MouseEvent.MOUSE_DOWN,onDLefMid);
				m_leftBottom.removeEventListener(MouseEvent.MOUSE_DOWN,onDLefBottom);
			}
			
			if(m_target)
			{
				m_target.stage.removeEventListener(MouseEvent.MOUSE_UP,onMouseStageUp);
				m_target.stage.removeEventListener(MouseEvent.MOUSE_MOVE,onMouseStageMove);
			}
			
			if(m_target)
			{
				if(!m_target.hasEventListener(Event.ENTER_FRAME))
				{
					m_target.removeEventListener(Event.ENTER_FRAME,targetMove)
				}
			}
			
			m_bottomMid = null;
			m_center = null;
			m_leftBottom = null;
			m_leftMid = null;
			m_leftTop = null;
			m_lines = null;
			m_rightBottom = null;
			m_rightMid = null;
			m_rightTop = null;
			m_target = null;
			m_topMid = null;
			m_dragPoint = null;
		}
		
		private function createTool() : void
		{
			m_lines = new TransformLinesTool;
			
			m_topMid = new TransformDotControl;
			m_bottomMid  = new TransformDotControl;
			m_rightTop  = new TransformDotControl;
			m_rightMid  = new TransformDotControl;
			m_rightBottom  = new TransformDotControl;
			m_leftBottom  = new TransformDotControl;
			m_leftMid  = new TransformDotControl;
			m_leftTop  = new TransformDotControl;
			m_center  = new TransformDotControl("cir");
			
			this.addChild(m_lines);
			
			this.addChild(m_topMid);
			this.addChild(m_bottomMid);
			this.addChild(m_rightTop);
			this.addChild(m_rightMid);
			this.addChild(m_rightBottom);
			this.addChild(m_leftBottom);
			this.addChild(m_leftMid);
			this.addChild(m_leftTop);
			this.addChild(m_center);
			
			m_center.addEventListener(MouseEvent.MOUSE_DOWN,onCenterDown);
			m_center.addEventListener(MouseEvent.MOUSE_UP,onCenterUp);
			
			m_topMid.addEventListener(MouseEvent.MOUSE_DOWN,onDTopMid);
			m_bottomMid.addEventListener(MouseEvent.MOUSE_DOWN,onDBottomMid);
			m_rightTop.addEventListener(MouseEvent.MOUSE_DOWN,onDRightTop);
			m_rightMid.addEventListener(MouseEvent.MOUSE_DOWN,onDRightMid);
			m_rightBottom.addEventListener(MouseEvent.MOUSE_DOWN,onDRightBottom);
			m_leftTop.addEventListener(MouseEvent.MOUSE_DOWN,onDLeftTop);
			m_leftMid.addEventListener(MouseEvent.MOUSE_DOWN,onDLefMid);
			m_leftBottom.addEventListener(MouseEvent.MOUSE_DOWN,onDLefBottom);
		}
		
		private var m_oldMP : Point
		private var m_curMP : Point;
		
		private function onMouseStageMove(evt : MouseEvent) : void
		{	
			evt.stopImmediatePropagation();
			evt.stopPropagation();
		
			m_oldMP = m_curMP;
			
			if(this.root)
			{
				m_curMP = new Point(this.root.mouseX,this.root.mouseY);
			}
			
			if(!m_oldMP)
			{
				m_oldMP = m_curMP;
			}
			
			if(m_strength && m_target)
			{
				var oldRP : Point;
				
				switch(true)
				{
					case m_strengthDot == m_leftTop:
						oldRP = new Point(m_target.x+m_target.width,m_target.y+m_target.height);

						m_target.width -= (m_curMP.x - m_oldMP.x);
						m_target.height -= (m_curMP.y - m_oldMP.y);
						
						trace(m_curMP.x - m_oldMP.x,m_target.width);
	
						m_target.x = oldRP.x - m_target.width;
						m_target.y = oldRP.y - m_target.height;
						break;
					case m_strengthDot == m_leftMid:
						oldRP = new Point(m_target.x+m_target.width,m_target.y+m_target.height);
						
						m_target.width -= (m_curMP.x - m_oldMP.x);
						
						m_target.x = oldRP.x - m_target.width;
						
						break;
					case m_strengthDot == m_leftBottom:
						oldRP = new Point(m_target.x+m_target.width,m_target.y);
						
						m_target.width -= (m_curMP.x - m_oldMP.x);
						m_target.height += (m_curMP.y - m_oldMP.y);
						
						
						m_target.x = oldRP.x - m_target.width;
						m_target.y = oldRP.y;
						
						break;
					case m_strengthDot == m_rightBottom:

						m_target.width += (m_curMP.x - m_oldMP.x);
						m_target.height += (m_curMP.y - m_oldMP.y);
						
						break;
					case m_strengthDot == m_rightMid:
						
						m_target.width += (m_curMP.x - m_oldMP.x);
						
						break;
					case m_strengthDot == m_rightTop:
						oldRP = new Point(m_target.x,m_target.y+m_target.height);
						
						m_target.width += (m_curMP.x - m_oldMP.x);
						m_target.height -= (m_curMP.y - m_oldMP.y);
						
						m_target.x = oldRP.x;
						m_target.y = oldRP.y - m_target.height;
						break;
					case m_strengthDot == m_topMid:
						oldRP = new Point(m_target.x+m_target.width,m_target.y+m_target.height);
						
						m_target.height -= (m_curMP.y - m_oldMP.y);
						
						m_target.x = oldRP.x - m_target.width;
						m_target.y = oldRP.y - m_target.height;
						break;
					case m_strengthDot == m_bottomMid:
						
						m_target.height += (m_curMP.y - m_oldMP.y);
				
						break;
				}
				
				draw();
			}
			
			if(m_target)
			{	
				setXY();
				if(m_lines)
				{
					m_lines.draw();
				}
			}
		}
		
		private function onMouseStageUp(evt : MouseEvent) : void
		{
			m_strength = false;
			m_strengthDot = null;
			m_lines.draw();
			
			if(m_target)
			{
				if(!m_target.hasEventListener(Event.ENTER_FRAME))
				{
					m_target.removeEventListener(Event.ENTER_FRAME,targetMove)
				}
			}
			
			this.dispatchEvent(new Event(CHANGED));
		}
		
		private function onDLeftTop(evt : MouseEvent) : void
		{
			m_strength = true;
			m_strengthDot = m_leftTop;
		}
		
		private function onDTopMid(evt : MouseEvent) : void
		{
			m_strength = true;
			m_strengthDot = m_topMid;
		}
		
		private function onDRightTop(evt : MouseEvent) : void
		{
			m_strength = true;
			m_strengthDot = m_rightTop;
		}
		
		private function onDRightMid(evt : MouseEvent) : void
		{
			m_strength = true;
			m_strengthDot = m_rightMid;
		}
		
		private function onDRightBottom(evt : MouseEvent) : void
		{
			m_strength = true;
			m_strengthDot = m_rightBottom;
		}
		
		private function onDBottomMid(evt : MouseEvent) : void
		{
			m_strength = true;
			m_strengthDot = m_bottomMid;
		}
		
		private function onDLefBottom(evt : MouseEvent) : void
		{
			m_strength = true;
			m_strengthDot = m_leftBottom;
		}
		
		private function onDLefMid(evt : MouseEvent) : void
		{
			m_strength = true;
			m_strengthDot = m_leftMid;
		}
		
		private function onCenterDown(evt : MouseEvent) : void
		{
			if(m_target)
			{
				m_target.addEventListener(Event.ENTER_FRAME,targetMove);
				
//				m_target.startDrag();
				m_dragPoint = new Point(m_target.mouseX,m_target.mouseY);
				setXY();
				draw();
			}
		}
		
		private function onCenterUp(evt : MouseEvent) : void
		{
			if(m_target)
			{
				m_target.removeEventListener(Event.ENTER_FRAME,targetMove);
				
//				m_target.stopDrag();
				setXY();
				draw();
			}
		}
		
		public function set target(value:Sprite) : void
		{
			m_target = value;
			
			if(value)
			{
				this.visible = true;
				
				if(!value.stage.hasOwnProperty(MouseEvent.MOUSE_MOVE))
					value.stage.addEventListener(MouseEvent.MOUSE_MOVE,onMouseStageMove);
				if(!value.stage.hasOwnProperty(MouseEvent.MOUSE_UP))
					value.stage.addEventListener(MouseEvent.MOUSE_UP,onMouseStageUp);
				if(m_target)
				{
					if(!m_target.hasEventListener(Event.ENTER_FRAME))
					{
						m_target.removeEventListener(Event.ENTER_FRAME,targetMove);	
					}
				}
				
				draw();
				setXY();
				
				if(m_lines)
				{
					m_lines.target = value;
					m_lines.draw();
				}
			}
			else
			{
				this.visible = false;
				if(m_target)
				{
					if(!m_target.hasEventListener(Event.ENTER_FRAME))
					{
						m_target.removeEventListener(Event.ENTER_FRAME,targetMove)
					}
				}
			}
		}
		
		private function targetMove(evt : Event) : void
		{
			if(!m_target) return;
			
			m_target.x = (m_target.parent.mouseX - m_dragPoint.x);
			m_target.y = (m_target.parent.mouseY - m_dragPoint.y);
		}
		
		public function get target() : Sprite
		{
			return m_target;
		}
		
		public function update() : void
		{
			draw();
			setXY();
			
			if(m_lines)
			{
				m_lines.draw();
			}
		}
		
		public  function draw():void
		{
			if(m_target)
			{
//				var rec : Rectangle = target.getBounds(target);
//				
//				this.graphics.clear();
//				this.graphics.beginFill(0x000000,0);
//				this.graphics.drawRect(rec.left,rec.top,rec.right,rec.bottom);
			}
		}
		
		private function setXY() : void
		{	
			if(!m_target) return;
			
			var p : Point = m_target.localToGlobal(new Point(0,0));
			
			p = this.parent.globalToLocal(p);
			
			this.x = p.x;
			this.y = p.y;
			
			
			if(!m_leftBottom) return;
			
			m_leftTop.x = 0;
			m_leftTop.y = 0;
			
			m_leftMid.x = 0;
			m_leftMid.y = m_target.height >> 1;
			
			m_leftBottom.x = 0;
			m_leftBottom.y = m_target.height;
			
			m_topMid.x = m_target.width >> 1;
			m_topMid.y = 0;
			
			m_center.x = m_target.width >> 1;
			m_center.y = m_target.height >> 1;
			
			m_bottomMid.x = m_target.width >> 1;
			m_bottomMid.y = m_target.height;
			
			m_rightBottom.x = m_target.width;
			m_rightBottom.y = m_target.height;
			
			m_rightMid.x = m_target.width;
			m_rightMid.y = m_target.height >> 1;
			
			m_rightTop.x = m_target.width;
			m_rightTop.y = 0;
		}
	}
}

import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.geom.Rectangle;

import untils.TransformTool2;

/*******************************************************/
 class TransformLinesTool extends Sprite
{
	private var m_target : Sprite;
	
	public function TransformLinesTool()
	{
		
	}
	
	public function get target() : Sprite
	{
		return m_target;
	}
	
	public function set target(value : Sprite) : void
	{
		m_target = value;
	}
	
	public function draw() : void
	{
		if(target)
		{
			var rec : Rectangle = target.getRect(target);
			
			this.graphics.clear();
			this.graphics.lineStyle(1,0xff0000);
			this.graphics.drawRect(0,0,target.width,target.height);
		}
	}
}

/*******************************************************/
class TransformDotControl extends Sprite
{
	private var m_type : String;
	
	public function TransformDotControl(type : String = "rec")
	{		
		m_type = type;
		draw();
	}
	
	public  function destory() : void
	{	
		m_type = null;
	}
	
	public  function draw() : void
	{
		this.graphics.clear();
		
		if(m_type == "cir")
		{
			this.graphics.beginFill(0xff0000);
			this.graphics.drawCircle(0,0,5);
		}
		else if(m_type == "rec")
		{
			this.graphics.beginFill(0x000000);
			this.graphics.drawRect(-5,-5,10,10);
		}
		
		this.graphics.endFill();
	}
}